var up_sURL="http://cache.static.userplane.com/presence";
var up_dURL="http://feed.presence.userplane.com/presence";
var up_wmURL="";
var up_cmdURL="";
var up_alwaysNotify=false;
var up_pServ="03.presence.userplane.com,04.presence.userplane.com";
function up_launch(_1){
    up_ow[_1]=up_ow[_1]==undefined?null:up_ow[_1];
    var _2=false;
    if(up_ow[_1]!=null){
        try{
            _2=up_ow[_1].opener==top&&!up_ow[_1].closed;
        }
        catch(e){
        }
    }
    if(up_ow[_1]==null||!_2){
        up_ow[_1]=window.open(up_wmURL+"?strDestinationUserID="+_1,"ICWindow_"+_1,"width=800,height=600,toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=1");
        if(up_ow[_1]==null){
            up_notify(_1);
        }else{
            up_clear(_1,false);
        }
    }
}
function up_clear(_3,_4){
    var l=up_la;
    up_la=new Array();
    var _6=false;
    if(_4){
        up_notifyOnClickedNo(_3);
    }
    while(l.length>0){
        var _7=l.pop();
        if(_7.uid!=_3){
            up_la.push(_7);
        }else{
            _6=true;
        }
    }
    if(_6||!_4){
        frames["up_lf"].location.href=up_dURL+"/o.php?domain_id="+up_domain_id+"&sid="+up_user_id+"&ou="+_3+"&forceClear="+(_4?"true":"false");
    }
    up_show();
}
function up_notify(_8){
    var _9=true;
    for(var i=0;i<up_la.length;i++){
        if(up_la[i].uid==_8){
            _9=false;
        }
    }

    if(_9){
        var _b=new Object();
        _b.uid=_8;
        
        lm_notify = function(originatorUsername,avatar) {
            var originator = originatorUsername || "A italki member";
            _b.n=originator;
            _b.img = avatar;
            
            up_la.push(_b);
            up_show();
        }

        lm_notifyWithUsername(_8, lm_notify);
    }
    else {
        up_show();
    }
}
function up_show(){
<div>
	<div>"+up_la[0].n+" wants to chat with you.</div>
	<div>
		<div style=\"float:left; width:80px\"><img width=\"60\" src=\""+up_la[0].img+"\" bord=\"0\"></div>
		<div style=\"float:left; width:80px\"><a style=\"font-size:larger;\" href=\"\" onClick=\"javascript: up_launch( '"+up_la[0].uid+"' ); return false;\">Accept</a></div>
		<div style=\"float:left; width:80px\"><a style=\"font-size:larger;\" href=\"\" onClick=\"javascript: up_clear( '"+up_la[0].uid+"', true ); return false;\">Decline</a></div>
		<div style=\"float:left; width:80px\">Profile</div>
	</div>
</div>
            up_uid_display=up_la[0].uid;
            up_animate(200);
        }
    }else{
        up_uid_display="";
        up_animate(-200);
    }
}


function up_show(){
    var e=document.getElementById("up_nd");
    if(up_la.length>0){
        if(up_uid_display!=up_la[0].uid){
            e.innerHTML="<div style=\"text-align:center\">"+(up_is_win_ie?"":"<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"center\">")+"<table border=\"0\" cellpadding=\"2\" cellspacing=\"5\"><tr><td nowrap align=\"center\"><strong style=\"font-size:larger;\">Incoming IM Message</strong></td></tr><tr><td align=\"center\"><br>Would you like to accept?</td></tr><tr><td nowrap align=\"center\"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td></tr></table>"+(up_is_win_ie?"":"</td></tr></table>")+"</div>";
            up_uid_display=up_la[0].uid;
            up_animate(200);
        }
    }else{
        up_uid_display="";
        up_animate(-200);
    }
}
function up_animate(dY){
    var e=document.getElementById("up_nd");
    if(up_divY!=dY||up_la.length>0){
        if(up_divY!=dY){
            up_divY+=dY<up_divY?-10:10;
        }
        var px=up_divY+document.body.scrollTop+"px";
        e.style.top=px;
        clearTimeout(up_at);
        up_at=setTimeout("up_animate("+dY+")",33);
    }else{
        e.style.top=dY;
    }
}
function up_clean(ins){
    var _11="";
    for(var i=0;i<ins.length;i++){
        var c=ins.charAt(i);
        if((c>="A"&&c<="Z")||(c>="a"&&c<="z")||(c>="0"&&c<="9")){
            _11+=c;
        }else{
            _11+="_";
        }
    }
    return _11;
}
function receiveData(_14){
    if(_14!=""){
        var a=_14.split(",");
        if(a.length>0){
            while(u=a.shift()){
                if(up_alwaysNotify){
                    up_notify(u);
                }else{
                    up_launch(u);
                }
            }
        }
    }
}
function URLencode(_16){
    return escape(_16).replace(/\+/g,"%2B").replace(/\"/g,"%22").replace(/\'/g,"%27").replace(/\//g,"%2F");
}
function up_runPresence(_17,_18){
    up_user_id=URLencode(_18);
    up_domain_id=_17;
    up_divY=-200;
    up_la=new Array();
    up_ow=new Object();
    up_uid_display="";
    document.write("<iframe name=\"up_lf\" id=\"up_lf\" style=\"position:absolute; top: -200px; z-index:9998; width:1px; height:1px; border: 0px\" src=\"\"></iframe>");
    document.write("<iframe name=\"up_cf\" id=\"up_cf\" style=\"position:absolute; top: -200px; z-index:9977; width:1px; height:1px; border: 0px\" src=\"\"></iframe>");
    document.write("<div id=\"up_nd\" style=\"position:absolute; width:250px; z-index:111111; left: 30px; top: -200px; background-color:#eeeeee; border: 1px solid #000000;\"></div>");
    if(up_user_id!=""){
        var _19="server="+up_pServ+"&uid="+up_user_id+"&domain_id="+up_domain_id+"&interval=10";
        document.write("<div id=\"up_presence_swf\" style=\"position:absolute; width:100px; z-index:9996; top: -200px;\"><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" id=\"presence\" width=\"1\" height=\"1\" align=\"middle\"><param name=\"allowScriptAccess\" value=\"anyDomain\" /><param name=\"movie\" value=\""+up_sURL+"/presence.swf\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#ffffff\" /><param name=\"flashvars\" value=\""+_19+"\" /><embed src=\""+up_sURL+"/presence.swf\" flashvars=\""+_19+"\" quality=\"high\" bgcolor=\"#ffffff\" width=\"1\" height=\"1\" swLiveConnect=true id=\"presence\" name=\"presence\" align=\"middle\" allowScriptAccess=\"anyDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /></object></div>");
    }
}
function up_notifyOnClickedNo(_1a){
    if(up_cmdURL!=""){
        frames["up_cf"].location.href=up_cmdURL+"?strDestinationUserID="+_1a+"&strEvent=User.ConversationRefused";
    }
}
var up_user_id=null;
var up_domain_id=null;
var up_divY=null;
var up_la=null;
var up_uid_display=null;
var up_at=null;
var up_ow=null;
var up_agt=navigator.userAgent.toLowerCase();
var up_appVer=navigator.appVersion.toLowerCase();
var up_is_mac=up_agt.indexOf("mac")!=-1;
var up_is_safari=up_agt.indexOf("safari")!=-1&&up_is_mac;
var up_is_khtml=up_is_safari||up_agt.indexOf("konqueror")!=-1;
var up_is_ie=up_appVer.indexOf("msie")!=-1&&up_agt.indexOf("opera")==-1&&!up_is_khtml;
var up_is_win=up_is_mac?false:(up_agt.indexOf("win")!=-1||up_agt.indexOf("16bit")!=-1);
var up_is_win_ie=up_is_win&&up_is_ie;


function lm_notifyWithUsername(userID, lm_notify) {
    var url = '/webchat/GetNickNameAndAvatar.aspx?UserID=' + userID;
    new Ajax.Request(url, {
        onSuccess: function(transport) {
            var username = transport.responseText.split("$");
            lm_notify(username[0],username[1]);
        },
        onFailure: function() { return null; }
    });
}

